<?php

namespace Kylinlxl\QiniuOss;

use Encore\Admin\Form\Field;

class OssWangEditor extends Field
{
    protected $view = 'qiniu-oss::oss-wangeditor';
    protected static $css = [
        'vendor/kylinlxl/qiniu-oss/wangEditor-3.0.10/wangEditor.min.css',
    ];
    protected static $js = [
        'vendor/kylinlxl/qiniu-oss/wangEditor-3.0.10/wangEditor.min.js',
        'vendor/kylinlxl/qiniu-oss/plupload-2.1.2/js/plupload.full.min.js',
        'vendor/kylinlxl/qiniu-oss/upload.js',
    ];
    public function render()
    {
        $name = $this->formatName($this->column);
        $token = csrf_token();
        $this->script = <<<EOT
(function(){
var editor = new window.wangEditor('#$name');
editor.customConfig.zIndex = 0;
editor.customConfig.uploadImgShowBase64 = true;
editor.customConfig.qiniu = true;
editor.customConfig.onchange = function (html) {
    $('input[name="$name"]').val(html);
}
editor.create();
init_wangeditor_upload(editor, '$token')
})();
EOT;
        return parent::render();
    }
}